package misc;

import io.web.JSONConnector;
import nl.preflag.basewars.R;
import activitys.BaseActionActivity;
import activitys.LogActivity;
import android.content.Context;
import android.content.Intent;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.google.android.maps.OverlayItem;

/**
 * @author Patrick van Gils, 0787862, INF3M
 * Balloon overlay
 */

public class BalloonOverlayView extends FrameLayout {

	private LinearLayout layout;
	private TextView title, snippet, health;
	private int base_id, base_type, player_id, base_health; // base_type 1=me, 2=friend, 3=enemy
	private ProgressBar pb;
	public JSONConnector webby;
	private Context context;
	
	/**
	 * Balloon overlay constrcutor
	 * 
	 * @param ct
	 * @param balloonBottomOffset
	 * @param bid
	 * @param pid
	 * @param bType
	 * @param bHealth
	 */
	public BalloonOverlayView(Context ct, int balloonBottomOffset, int bid, int pid, int bType, int bHealth) {

		super(ct);

		base_id = bid;
		player_id = pid;
		base_type = bType;
		base_health = bHealth;
		context = ct;
		
		setPadding(10, 0, 10, balloonBottomOffset);
		layout = new LinearLayout(context);
		layout.setVisibility(VISIBLE);
		
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		switch(base_type) {
			case 1:
				View me = inflater.inflate(R.layout.balloon_map_my_overlay, layout);
				pb = (ProgressBar) me.findViewById(R.id.balloon_health_bar);
				pb.setVisibility(ProgressBar.VISIBLE);
				pb.setProgress(base_health);
				health = (TextView) me.findViewById(R.id.balloon_health_textview);
				health.setText(base_health + " % health");
				title = (TextView) me.findViewById(R.id.balloon_item_title);
				snippet = (TextView) me.findViewById(R.id.balloon_item_snippet);

				// Set the buttons 
				ImageView closeMe = (ImageView) me.findViewById(R.id.close_img_button);
				closeMe.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						layout.setVisibility(GONE);
					}
				});
				
				// Set the buttons 
				ImageView logButtonMe = (ImageView) me.findViewById(R.id.log_img_button);
				logButtonMe.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						openLog();
					}
				});
				
				
				ImageView upgradeMe = (ImageView) me.findViewById(R.id.balloonUpgradeButton);
				upgradeMe.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						baseAction(base_type);
					}
				});
				break;
			case 2:
				View friend = inflater.inflate(R.layout.balloon_map_my_overlay, layout);
				pb = (ProgressBar) friend.findViewById(R.id.balloon_health_bar);
				pb.setVisibility(ProgressBar.VISIBLE);
				pb.setProgress(base_health);
				health = (TextView) friend.findViewById(R.id.balloon_health_textview);
				health.setText(base_health + " % health");
				title = (TextView) friend.findViewById(R.id.balloon_item_title);
				snippet = (TextView) friend.findViewById(R.id.balloon_item_snippet);
				
				// Set the buttons 
				ImageView closeFriend = (ImageView) friend.findViewById(R.id.close_img_button);
				closeFriend.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						layout.setVisibility(GONE);
					}
				});
				
				ImageView upgradeFriend = (ImageView) friend.findViewById(R.id.balloonUpgradeButton);
				upgradeFriend.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						baseAction(base_type);
					}
				});
				break;
			case 3:
				View enemy = inflater.inflate(R.layout.balloon_map_enemy_overlay, layout);
				pb = (ProgressBar) enemy.findViewById(R.id.balloon_health_bar_enemy);
				pb.setVisibility(ProgressBar.VISIBLE);
				pb.setProgress(base_health);
				health = (TextView) enemy.findViewById(R.id.balloon_health_textview_enemy);
				health.setText(base_health + " % health");
				title = (TextView) enemy.findViewById(R.id.balloon_item_title_enemy);
				snippet = (TextView) enemy.findViewById(R.id.balloon_item_snippet_enemy);
				
				// Set the buttons 
				ImageView closeEnemy = (ImageView) enemy.findViewById(R.id.close_img_button_enemy);
				closeEnemy.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						layout.setVisibility(GONE);
					}
				});
				
				ImageView attackEnemy = (ImageView) enemy.findViewById(R.id.balloonAttackButton);
				attackEnemy.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						baseAction(base_type);
					}
				});
				break;
		}
		FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		params.gravity = Gravity.NO_GRAVITY;

		addView(layout, params);

	}
	
	/**
	 * Start enemy attack actvity
	 */
	private void baseAction(int type) {
		Intent mmIntent = new Intent(context, BaseActionActivity.class);
		mmIntent.putExtra("base_id", base_id);
		mmIntent.putExtra("player_id", player_id);
		mmIntent.putExtra("base_type", base_type);
		context.startActivity(mmIntent);
	}
	
	/**
	 * Open combat log activity
	 */
	private void openLog() {
		Intent mmIntent = new Intent(context, LogActivity.class);
		mmIntent.putExtra("base_id", base_id);
		context.startActivity(mmIntent);
	}
	
	/**
	 * Set the data
	 * @param item
	 */
	public void setData(OverlayItem item) {
		
		layout.setVisibility(VISIBLE);
		if (item.getTitle() != null) {
			title.setVisibility(VISIBLE);
			title.setText(item.getTitle());
			
		} else {
			title.setVisibility(GONE);
		}
		if (item.getSnippet() != null) {
			snippet.setVisibility(VISIBLE);
			snippet.setText(item.getSnippet());
		} else {
			snippet.setVisibility(GONE);
		}
	}
}
